package main

import (
	"fmt"
	"net"
	"time"
)

func main() {
	fmt.Println("client start...")

	time.Sleep(1 * time.Second)

	// 1. 直接连接远程服务器, 得到一个conn链接
	conn, err := net.Dial("tcp", "127.0.0.1:9000")
	if err != nil {
		fmt.Println("client start err, ", err)
		return
	}

	for {
		// 2. 连接调用write写数据
		_, err := conn.Write([]byte("Hello zinx v0.1"))
		if err != nil {
			fmt.Println("write conn err, ", err)
			return
		}

		buf := make([]byte, 512)
		len, err := conn.Read(buf)
		if err != nil {
			fmt.Println("read buf err, ", err)
			return
		}

		fmt.Printf("server call back: %s, len: %d\n", buf, len)

		// 防止CPU跑满
		time.Sleep(1 * time.Second)

	}

}
